#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Tags

#End Region

Partial Class Usercontrols_Tags_PageTags
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Public Property ItemGuid() As Guid
        Get
            Return GetGuidFromVS("ItemGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ItemGuid", value)
        End Set
    End Property

    Public Property TagType() As Integer
        Get
            Return GetIntegerFromVS("TagType")
        End Get
        Set(ByVal value As Integer)
            ViewState.Add("TagType", value)
        End Set
    End Property

    Public Property HideData() As Boolean
        Get
            Return GetBooleanFromVS("HideData")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("HideData", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If ItemGuid = Guid.Empty Then
            GetQueryStringValues()
        End If

        SetPageState()

        If Me.Page.IsPostBack = False Then
            BindDataItems()
        End If

        Me.uxAddTagControlsPanel.Visible = IsUserAuthenticated()

        If IsUserAuthenticated() Then
            Me.uxTagTitleLabel.Text = "Your tags for this item"
        Else
            Me.uxTagTitleLabel.Text = "Tags"
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveTag()

        Dim Item As Tag = Tag.NewTag

        Item.itemid = ItemGuid
        Item.Userid = CurrentUserID
        Item.Tagname = Me.uxAddTagTextBox.Text.Trim
        Item.ivTagType = TagType
        Item.Save()

        BindDataItems()

    End Sub

#End Region

#Region " Load Data "

    Public Overloads Overrides Sub BindDataItems()

        If Not HideData Then
            If Not (ItemGuid = Guid.Empty) Then
                Dim List As New TagList
                Dim DT As New DataTable

                If IsUserAuthenticated Then
                    DT = List.GetTagsByUser(ItemGuid, CurrentUserID)
                Else
                    DT = List.GetTagsByPage(ItemGuid)
                End If

                With Me.uxTagsRepeater
                    .DataSource = DT
                    .DataBind()
                End With
            End If
        End If

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddTagLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddTagLinkButton.Click

        SaveTag()

    End Sub

    Protected Sub DeleteTag(ByVal sender As Object, ByVal e As CommandEventArgs)

        Tag.DeleteTag(ItemGuid, CurrentUserID, e.CommandArgument.ToString)
        BindDataItems()

    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ItemGuid = GetGuidFromQueryString("id")
    End Sub

    Private Sub SetPageState()

        Me.uxLoggedInPanel.Visible = IsUserAuthenticated()
        Me.uxPleaseLoginPanel.Visible = Not IsUserAuthenticated()

    End Sub

#End Region

End Class
